/**
 * All rights Reserved, Designed By https://www.pcitech.com
 *
 * @Title: @IndexController
 * @Package @com.suntek.device.viid.controller
 * @Description: 这里填写描述
 * @author: lyp林永坡
 * @date: @2020年9月11日 @上午9:13:42
 * @version v1.0
 * @Copyright: 2019 https://www.pcitech.com Inc. All rights reserved.
 * 注意：本内容仅限于佳都新太科技股份有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.dxy.client.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dxy.client.client.SocketClient;
import com.dxy.client.util.ReturnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = "测试")
@RestController
@RequestMapping("/socket")
public class TestController implements InitializingBean {

    @Value("${socket.server-ip}")
    private String serverIp;
    @Value("${socket.server-port}")
    private int serverPort;

    private SocketClient socketClient;

    @Override
    public void afterPropertiesSet() throws Exception {
        socketClient = new SocketClient();
        socketClient.setServerIp(serverIp);
        socketClient.setServerPort(serverPort);
        socketClient.connect();
    }

    @ApiOperation("发送消息")
    @GetMapping("sendMessage")
    public Object sendMessage(String enterpriseId, String dataId, String report) {
        JSONObject msg = new JSONObject();
        msg.put("enterpriseId", enterpriseId);
        msg.put("dataId", dataId);
        msg.put("report", report);
        String response = socketClient.send(msg.toJSONString());
        return ReturnUtil.ok(JSON.parseObject(response));
    }

    @ApiOperation("判断socket是否可用")
    @GetMapping("isSocketEnable")
    public Object isSocketEnable() {
        return ReturnUtil.ok(socketClient.isSocketEnable());
    }

}
